﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameEngine
{
    public class AnimationSequence
    {
        #region Attributes

        public int currentFrame {get; set;}
        public string Name { get; set;}
        public string NextAnimation { get; set; }
        public bool FinishedAnimation { get; set; }
        public bool LoopAnimation {get; set;}

        public Rectangle FirstFrame { get; set; }
        public int FrameCount { get; set; }

        private float _frameTimer = 0f;
        private float _frameDelay = 0.05f;

        #endregion

        #region Properties

        public float FrameTimer
        {
            get { return this._frameTimer; }
            set { this._frameTimer = value; }
        }

        public float FrameDelay
        {
            get { return this._frameDelay; }
            set { this._frameDelay = value; }
        }

        public Rectangle FrameRectangle
        {
            get
            {
                return new Rectangle(currentFrame * FirstFrame.Width, FirstFrame.Y, FirstFrame.Width, FirstFrame.Height);
            }
        }


        #endregion

        #region Constructor

        /// <summary>
        /// A instruction class do handle animated game objects.
        /// Each animation shall only represented by one spritesheet and in one line 
        /// </summary>
        /// <param name="texture">Target spritesheet</param>
        /// <param name="frameWidth">How wide each frame is in the animation</param>
        /// <param name="loopAnimation">Should the animation be looped</param>
        /// <param name="name">The name of the action the animation represents</param>
        public AnimationSequence(Rectangle firstFrame, int frameCount, bool loopAnimation, string name)
        {
            FirstFrame = firstFrame;
            FrameCount = frameCount;
            Name = name;
            FinishedAnimation = false;
            LoopAnimation = loopAnimation;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Starts playing this animation
        /// </summary>
        public void Play()
        {
            if (FinishedAnimation)
            {
                currentFrame = 0;
                FinishedAnimation = false;
            }
        }

        /// <summary>
        /// Iterates through the positions in the spritesheet with a delay between each frame
        /// if the animation is set to loop it restart at the first frame after one iteration
        /// else it only iterates one time
        /// </summary>
        /// <param name="gameTime">The game clock that is used to determen the delay</param>
        public void Update(GameTime gameTime)
        {
            FrameTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (FrameTimer >= FrameDelay)
            {
                currentFrame++;

                if (currentFrame >= FrameCount)
                {
                    if (LoopAnimation)
                        currentFrame = 0;
                    else
                    {
                        currentFrame = FrameCount - 1;
                        FinishedAnimation = true;
                    }
                }
                FrameTimer = 0f;
            }
        }

        #endregion
    }
}
